#!/bin/bash

DATE=`date +%Y_%m_%d`
DB_USER="mongodb"
DB_PASSWD="pass562"
DAYS=7

TAR_BAK="mongod_bak_$DATE.tar.gz"

cd /opt/mongod_bak/mongod_bak_now/

rm -rf /opt/mongod_bak/mongod_bak_now/*

mkdir -p /opt/mongod_bak/mongod_bak_now/$DATE

#backup all database
mongodump -u $DB_USER -p $DB_PASSWD -o /opt/mongod_bak/mongod_bak_now/$DATE

tar -zcvf /opt/mongod_bak/mongod_bak_list/$TAR_BAK /opt/mongod_bak/mongod_bak_now//$DATE

#Remove Recent Files $DAYS days ago
find /opt/mongod_bak/mongod_bak_list/ -mtime +$DAYS -delete